package com.zyj.myapplication.Activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.zyj.myapplication.Fragment.AllOrderFragment;
import com.zyj.myapplication.Fragment.FinOrderFragment;
import com.zyj.myapplication.Fragment.NfinOrderFragment;
import com.zyj.myapplication.R;

public class UserOrderActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mAllorder;
    private LinearLayout mFinorder;
    private LinearLayout mNfinorder;
    private ImageView orderlist_return;
    private Fragment mTab1;
    private Fragment mTab2;
    private Fragment mTab3;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userorder);
        LinearLayoutManager LL = new LinearLayoutManager(this);
        initView();//初始化
        initEvent();//初始化事件
        setSelect(0);//
    }

    private void initEvent() {
        mAllorder.setOnClickListener(this);
        mFinorder.setOnClickListener(this);
        mNfinorder.setOnClickListener(this);
        orderlist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initView() {
        mAllorder = (LinearLayout) findViewById(R.id.order_all);
        mFinorder = (LinearLayout) findViewById(R.id.order_finish);
        mNfinorder = (LinearLayout) findViewById(R.id.order_nofinish);
        orderlist_return = (ImageView) findViewById(R.id.orderlist_return);

    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (mTab1 == null) {
                    mTab1 = new AllOrderFragment();
                    transaction.add(R.id.id_content2, mTab1);
                } else {
                    transaction.show(mTab1);
                }
                break;
            case 1:
                if (mTab2 == null) {
                    mTab2 = new FinOrderFragment()
                    ;
                    transaction.add(R.id.id_content2, mTab2);
                } else {
                    transaction.show(mTab2);

                }
                break;
            case 2:
                if (mTab3 == null) {
                    mTab3 = new NfinOrderFragment();
                    transaction.add(R.id.id_content2, mTab3);
                } else {
                    transaction.show(mTab3);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab1 != null) {
            transaction.hide(mTab1);
        }
        if (mTab2 != null) {
            transaction.hide(mTab2);
        }
        if (mTab3 != null) {
            transaction.hide(mTab3);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_all:
                setSelect(0);
                break;
            case R.id.order_finish:
                setSelect(1);
                break;
            case R.id.order_nofinish:
                setSelect(2);
                break;
            default:
                break;
        }
    }
}
